package cn.iocoder.yudao.module.sys.controller.admin.instrumentoperationlog;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.sys.controller.admin.instrumentoperationlog.vo.*;
import cn.iocoder.yudao.module.sys.dal.dataobject.instrumentoperationlog.InstrumentOperationLogDO;
import cn.iocoder.yudao.module.sys.service.instrumentoperationlog.InstrumentOperationLogService;

@Tag(name = "管理后台 - 仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）")
@RestController
@RequestMapping("/sys/instrument-operation-log")
@Validated
public class InstrumentOperationLogController {

    @Resource
    private InstrumentOperationLogService instrumentOperationLogService;

    @PostMapping("/create")
    @Operation(summary = "创建仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）")
    @PreAuthorize("@ss.hasPermission('sys:instrument-operation-log:create')")
    public CommonResult<Long> createInstrumentOperationLog(@Valid @RequestBody InstrumentOperationLogSaveReqVO createReqVO) {
        return success(instrumentOperationLogService.createInstrumentOperationLog(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）")
    @PreAuthorize("@ss.hasPermission('sys:instrument-operation-log:update')")
    public CommonResult<Boolean> updateInstrumentOperationLog(@Valid @RequestBody InstrumentOperationLogSaveReqVO updateReqVO) {
        instrumentOperationLogService.updateInstrumentOperationLog(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('sys:instrument-operation-log:delete')")
    public CommonResult<Boolean> deleteInstrumentOperationLog(@RequestParam("id") Long id) {
        instrumentOperationLogService.deleteInstrumentOperationLog(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）")
                @PreAuthorize("@ss.hasPermission('sys:instrument-operation-log:delete')")
    public CommonResult<Boolean> deleteInstrumentOperationLogList(@RequestParam("ids") List<Long> ids) {
        instrumentOperationLogService.deleteInstrumentOperationLogListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('sys:instrument-operation-log:query')")
    public CommonResult<InstrumentOperationLogRespVO> getInstrumentOperationLog(@RequestParam("id") Long id) {
        InstrumentOperationLogDO instrumentOperationLog = instrumentOperationLogService.getInstrumentOperationLog(id);
        return success(BeanUtils.toBean(instrumentOperationLog, InstrumentOperationLogRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）分页")
    @PreAuthorize("@ss.hasPermission('sys:instrument-operation-log:query')")
    public CommonResult<PageResult<InstrumentOperationLogRespVO>> getInstrumentOperationLogPage(@Valid InstrumentOperationLogPageReqVO pageReqVO) {
        PageResult<InstrumentOperationLogDO> pageResult = instrumentOperationLogService.getInstrumentOperationLogPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, InstrumentOperationLogRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯） Excel")
    @PreAuthorize("@ss.hasPermission('sys:instrument-operation-log:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportInstrumentOperationLogExcel(@Valid InstrumentOperationLogPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<InstrumentOperationLogDO> list = instrumentOperationLogService.getInstrumentOperationLogPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "仪器设备操作记录表（记录新增、修改、删除、出库、入库操作，支持追溯）.xls", "数据", InstrumentOperationLogRespVO.class,
                        BeanUtils.toBean(list, InstrumentOperationLogRespVO.class));
    }

}